xxxxxxxxxx4
1
begin2
using Plots3
using Images4
using LinearAlgebra5
endxxxxxxxxxx1
1
begin2
img = load("../data/dog.jpg")3
X = convert(Array{Float64}, Gray.(img))4
plot(img)5
endx
1
begin2
(U, Σ, V) = svd(X, full=false)3
S = Diagonal(Σ)4
Vt = V'5
rs = [length(Σ), 5, 20, 100]6
p_1 = plot(layout=length(rs), size=(600, 600))7
for i ∈ 1:length(rs)8
r = rs[i]9
Xapprox = U[:, 1:r] * S[1:r, 1:r] * Vt[1:r, :]10
plot!(p_1[i], Gray.(Xapprox), axis=nothing)11
percentage = round(r*100/length(Σ), digits=2)12
title!(p_1[i], "r=$(r) $(percentage)%")13
14
end15
p_116
endChapter 1 Exercise 2/2
x
1
begin2
3
p_2 = plot(layout=2, legend=false)4
plot!(p_2[1], Σ, yaxis=:log)5
title!(p_2[1], "Singular Values")6
7
plot!(p_2[2], cumsum(Σ) ./ sum(Σ))8
title!(p_2[2], "Singular Values:\nCumulative Sum")9
p_210
end